package com.xinqi.modules.user.users.user.manager.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.redis.constants.UserCacheConstant;
import com.xinqi.common.redis.utils.Caches;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.user.user.domain.UserEntity;
import com.xinqi.modules.user.user.dto.req.SearchFriendDTO;
import com.xinqi.modules.user.user.dto.rsp.SearchFriendResultDTO;
import com.xinqi.modules.user.user.dto.rsp.UserLoginResultDTO;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.utils.spring.Spring;
import com.xinqi.modules.user.users.user.convert.UserConvert;
import com.xinqi.modules.user.users.user.manager.UserManager;
import com.xinqi.modules.user.users.user.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 用户信息管理层实现
 *
 * @author: wenjie
 * @date: 2023/05/17
 */
@Service
@RequiredArgsConstructor
public class UserManagerImpl extends BaseServiceImpl<UserMapper, UserEntity> implements UserManager {

    private final RedissonClient redissonClient;
    private final UserConvert userConvert;

    /**
     * 根据id查询 用户信息数据
     */
    @Override
    public UserEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        // 添加缓存
        return Caches.get(() -> this.baseMapper.selectById(id), redissonClient, UserCacheConstant.USER_KEY, id);
    }

    /**
     * 用户更新，重写
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public boolean updateById(UserEntity entity) {
        boolean b = SqlHelper.retBool(this.baseMapper.updateById(entity));
        Spring.committed(() -> {
            if (b) {
                // 清理缓存
                Caches.del(redissonClient, UserCacheConstant.USER_KEY, entity.getId());
                // 更新数据
                LoginHelper.updateXqLoginUser(userConvert.account(findById(entity.getId())));
            }
        });
        return b;
    }

    /**
     * 根据id删除 用户信息数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        boolean b = this.baseMapper.deleteById(id) > 0;
        Spring.committed(() -> {
            if (b) {
                // 清理缓存
                Caches.del(redissonClient, UserCacheConstant.USER_KEY, id);
                // 退出登录
                Spring.committed(() -> LoginHelper.logout(id));
            }
        });
        return b;
    }

    /**
     * 根据手机号码查询，包含已注销
     *
     * @param mobile
     * @return
     */
    @Override
    public List<UserEntity> findByMobileIncludeDelete(String mobile) {
        return this.baseMapper.findByMobileIncludeDelete(mobile);
    }

    /**
     * 关键字搜索 -- 新奇号、手机号精准查询
     *
     * @param dto
     * @return
     */
    @Override
    public List<SearchFriendResultDTO> selectUsersByKeyword(SearchFriendDTO dto) {
        return this.baseMapper.selectUsersByKeyword(dto);
    }

    /**
     * 登录查询用户所有信息
     * @param mobile
     * @return
     */
    @Override
    @DS("slaver")
    public UserLoginResultDTO selectUserLoginByMobile(String mobile){
        return this.baseMapper.selectUserLoginByMobile(mobile);
    }

    /**
     * 登录查询用户所有信息通过ID
     * @param id
     * @return
     */
    @Override
    @DS("slaver")
    public UserLoginResultDTO selectUserLoginById(Long id) {
        return this.baseMapper.selectUserLoginById(id);
    }
}
